library(tidyverse)
library(estimatr)
library(stargazer)
library(texreg)
library(xtable)

setwd('/Users/alexandersahn/Library/CloudStorage/GoogleDrive-alexander.sahn@gmail.com/My Drive/Projects/Traceability/replication_data')
out <- ('./output')
source('./code/rename_fx.R')

data <- read_csv('./data/State Legislative Staffer_September 20, 2021_10.12.csv')
district <- read_csv('./data/firstcut_competitive.csv')

key <- data[1,] %>%
  t() %>%
  as.data.frame()
key$varname = rownames(key)

data <- data[c(-1,-2),]

stacked_data <- data %>%
  mutate(issue = localvignette1,
         prob_trace = case_when(str_detect(`v1-traceability`, 'difficult time') ~ 'difficult',
                                str_detect(`v1-traceability`, 'easy time') ~ 'easy'),
         simplicity = case_when(str_detect(`v1-solcomplexity`, 'can directly') ~ 'simple',
                                str_detect(`v1-solcomplexity`, 'cannot directly') ~ 'complex'),
         solve_prob = as.numeric(str_remove(`v1-probsolve`, '%'))/100,
         solu_trace = case_when(str_detect(`v1-soltraceability`, 'transparent') ~ 'easy',
                                str_detect(`v1-soltraceability`, 'indirect') ~ 'difficult'),
         cobenefits = case_when(str_detect(`v1-poleffect`, 'increase') ~ 'costs',
                                str_detect(`v1-poleffect`, 'decrease') ~ 'benefits'),
         time = case_when(str_detect(`v1-timetosolve`, 'six months') ~ .5,
                          str_detect(`v1-timetosolve`, 'two years') ~ 2,
                          str_detect(`v1-timetosolve`, 'four years') ~ 4,
                          str_detect(`v1-timetosolve`, 'ten years') ~ 10,
                          str_detect(`v1-timetosolve`, '15 years or more') ~ 15),
         dv_ease = case_when(Q84=='Very difficult' ~ 0,
                             Q84=='Difficult' ~ 1,
                             Q84=='Neither easy or difficult' ~ 2,
                             Q84=='Easy' ~ 3,
                             Q84=='Very easy' ~ 4),
         dv_likely = case_when(Q85=='Very unlikely' ~ 0,
                               Q85=='Unlikely' ~ 1,
                               Q85=='Neither likely or unlikely' ~ 2,
                               Q85=='Likely' ~ 3,
                               Q85=='Very likely' ~ 4),
         dv_priority = case_when(Q86=='Would not prioritize' ~ 0,
                                 Q86=='Low priorty' ~ 1,
                                 Q86=='Medium priority' ~ 2,
                                 Q86=='High priority' ~ 3),
         dv_elections = case_when(Q87=='Not at all' ~ 0,
                                  Q87=='Only a little' ~ 1,
                                  Q87=='A moderate amount' ~ 2,
                                  Q87=='A great deal' ~ 3)) %>%
  bind_rows(data %>%
              mutate(issue = `v2-topicshort`,
                     prob_trace = case_when(str_detect(`v2-probtraceability`, 'cannot easily') ~ 'difficult',
                                            str_detect(`v2-probtraceability`, 'can easily') ~ 'easy'),
                     simplicity = case_when(str_detect(`v2-solcomplexity`, 'fixing') ~ 'simple',
                                            str_detect(`v2-solcomplexity`, 'complex') ~ 'complex'),
                     solve_prob = as.numeric(str_remove(`v2-probsolve`, '%'))/100,
                     solu_trace = case_when(str_detect(`v2-soltraceability`, 'indirect') ~ 'difficult',
                                            str_detect(`v2-soltraceability`, 'transparent') ~ 'easy'),
                     cobenefits = case_when(str_detect(`v2-poleffect`, 'increase') ~ 'costs',
                                            str_detect(`v2-poleffect`, 'decrease') ~ 'benefits'),
                     time = case_when(str_detect(`v2-timetosolve`, 'six months') ~ .5,
                                      str_detect(`v2-timetosolve`, 'two years') ~ 2,
                                      str_detect(`v2-timetosolve`, 'four years') ~ 4,
                                      str_detect(`v2-timetosolve`, 'ten years') ~ 10,
                                      str_detect(`v2-timetosolve`, '15 years or more') ~ 15),
                     dv_ease = case_when(Q92=='Very difficult' ~ 0,
                                         Q92=='Difficult' ~ 1,
                                         Q92=='Neither easy or difficult' ~ 2,
                                         Q92=='Easy' ~ 3,
                                         Q92=='Very easy' ~ 4),
                     dv_likely = case_when(Q91=='Very unlikely' ~ 0,
                                           Q91=='Unlikely' ~ 1,
                                           Q91=='Neither likely or unlikely' ~ 2,
                                           Q91=='Likely' ~ 3,
                                           Q91=='Very likely' ~ 4),
                     dv_priority = case_when(Q90=='Would not prioritize' ~ 0,
                                             Q90=='Low priorty' ~ 1,
                                             Q90=='Medium priority' ~ 2,
                                             Q90=='High priority' ~ 3),
                     dv_elections = case_when(Q89=='Not at all' ~ 0,
                                              Q89=='Only a little' ~ 1,
                                              Q89=='A moderate amount' ~ 2,
                                              Q89=='A great deal' ~ 3))) %>%
  left_join(district %>%
              select(ResponseId, Trump_house, Clinton_house, Trump_senate, Clinton_senate)) %>% 
  mutate(pid_simple = case_when(str_detect(party, 'Democrat') ~ "D",
                                str_detect(party, 'Republican') ~ "R",
                                str_detect(party, 'Independent') ~ "I"),
         competitive = ifelse(abs(Trump_house - Clinton_house)<=.1, T, F),
         competitive = ifelse(is.na(competitive), abs(Trump_senate - Clinton_senate)<=.1, competitive)) %>%
  mutate_at(vars(dv_ease:dv_elections), function(x){x/4}) %>%
  rowwise() %>% 
  mutate(dv_index = (dv_ease + dv_likely + dv_priority + dv_elections)/4) %>% 
  mutate(time=time/15) 

reg_data %>%
  mutate(panel = case_when(name=="Index" ~ "Index",
                           T ~ "Individual Variables"),
         name = ordered(name, levels = c("Index", "Ease", "Electoral\nImpact", "Likelihood", 'Priority'))) %>%
  ggplot(aes(col = name, shape = name)) + 
  geom_hline(yintercept=0, lty=2) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.96*std.error), ymax=(estimate + 1.96*std.error)), position=position_dodge(width=.3)) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.65*std.error), ymax=(estimate + 1.65*std.error)), fatten=1.5, size=1.1, position=position_dodge(width=.3)) + 
  coord_flip() +  
  facet_wrap(~panel) + 
  labs(x='', y = '') +
  scale_color_viridis_d(breaks = c('Ease', "Electoral\nImpact", "Likelihood", 'Priority')) + 
  scale_shape_discrete(breaks = c('Ease', "Electoral\nImpact", "Likelihood", 'Priority')) + 
  theme_classic() +
  theme(legend.position='bottom', legend.title=element_blank()) 
ggsave(paste0(out,'/elite_main_index.pdf'), height=5, width=5)

stargazer(reg_data %>% 
            select(DV = name, Coefficient = term, estimate, std.error) %>%
            mutate(estimate = round(estimate, 3),
                   std.error = round(std.error, 3)), 
          summary=F, out = paste0(out, '/elite_main.tex'), style='apsr', digits=2, rownames=F, 
          title = '\\textbf{Tabular Presentation of Elite Experiment}',
          label = "tab:elite_main")

# factor variables for time and prob
print(xtable(reg_data %>% 
               mutate(estimate = round(estimate, 3),
                      std.error = round(std.error, 3)) %>% 
               select(DV = name, Coefficient = term, Estimate = estimate, `Std. Error` = std.error),
             caption = '\\textbf{Tabular Presentation of Elite Experiment, Using Factor Variables}',
             caption.above = T,
             label = "tab:elite_main_factor"),
      file = paste0(out, '/elite_main_factor.tex'),
      include.rownames=FALSE)


# reliability
stacked_data %>%
  ungroup() %>% 
  select(dv_ease, dv_likely, dv_priority, dv_elections) %>%
  psych::alpha()

pca <- stacked_data %>%
  ungroup() %>% 
  select(dv_ease, dv_likely, dv_priority, dv_elections) %>%
  filter(!is.na(dv_ease) & !is.na(dv_likely) & !is.na(dv_priority) & !is.na(dv_elections)) %>%
  prcomp(rank=2)

pca$rotation

# interaction model

r1 <- lm_robust(dv_index ~ prob_trace + simplicity + solve_prob + solu_trace*cobenefits + time,
          fixed_effects = ~ issue, 
          data = stacked_data)
r1

stacked_data %>%
  select(issue:dv_index) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  group_by(name) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace*cobenefits + time,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  filter(str_detect(term, 'solu_trace|cobenefits'))

lm_robust(dv_index ~ prob_trace + simplicity + solve_prob + cobenefits + time,
                    fixed_effects = ~ issue, 
                    data = stacked_data %>%
            filter(solu_trace=='easy')) %>%
  tidy() %>% 
  mutate(solu_trace = 'easy') %>%
  bind_rows(lm_robust(dv_index ~ prob_trace + simplicity + solve_prob + cobenefits + time,
                      fixed_effects = ~ issue, 
                      data = stacked_data %>%
                        filter(solu_trace=='difficult')) %>%
              tidy() %>% 
            mutate(solu_trace = 'difficult')) %>%
  filter(term=='cobenefitscosts')
    
stacked_data %>%
  filter(cobenefits=='benefits') %>% 
  select(issue:dv_index) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  group_by(name) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + time,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  mutate(cobenefits = 'benefits') %>%
  bind_rows(stacked_data %>%
              filter(cobenefits == 'costs') %>% 
              select(issue:dv_index) %>%
              pivot_longer(cols=starts_with('dv_')) %>%
              group_by(name) %>%
              do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + time,
                                fixed_effects = ~ issue, 
                                data = .))) %>%
              mutate(cobenefits = 'costs')) %>%
  filter(term == 'solu_traceeasy') %>%
  arrange(name)

lm_robust(dv_index ~ prob_trace + simplicity + solve_prob + solu_trace + time,
          fixed_effects = ~ issue, 
          data = stacked_data %>%
            filter(cobenefits=='costs')) %>%
  tidy() %>% 
  mutate(cobenefits = 'costs') %>%
  bind_rows(lm_robust(dv_index ~ prob_trace + simplicity + solve_prob + solu_trace + time,
                      fixed_effects = ~ issue, 
                      data = stacked_data %>%
                        filter(cobenefits=='benefits')) %>%
              tidy() %>% 
              mutate(cobenefits = 'benefits')) %>%
  filter(term=='solu_traceeasy')

stacked_data %>%
  select(issue:dv_index) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  group_by(name) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + cobenefits*solu_trace + time,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  filter(term %in% c('solu_traceeasy', 'cobenefitscosts', 'cobenefitscosts:solu_traceeasy', '(Intercept)'))

stacked_data %>%
  select(issue:dv_index) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  group_by(name) %>%
  do(tidy(lm_robust(value ~ prob_trace*solu_trace + simplicity + solve_prob + cobenefits + time,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  filter(term %in% c('solu_traceeasy', 'prob_traceeasy', 'prob_traceeasy:solu_traceeasy'))

r2 <- lm_robust(dv_index ~ prob_trace*solu_trace + simplicity + solve_prob + cobenefits + time,
          fixed_effects = ~ issue, 
          data = stacked_data) 

texreg(list(r1,r2),
       stars = NULL,
       digits = 3,
       include.ci=F,
       #omit.coef = 'Intercept',
       #custom.model.names = c('Index', "Importance", "Approval", "Likelihood", "Priority"),
       custom.coef.names=c('Easy to Trace Problem', 'Simple Solution', 'Prob. of Solving', 'Easy to Trace Solution', 'Neg. Side Effects', 'Time to Solve', 'Solution Trace x Neg. Fx', 'Solution Trace x Problem Trace'),
       caption = '\\textbf{Elite Models with Interaction Effects}',
       #groups = list('Comments' = 3:5),
       label = "tab:intx",
       #custom.gof.rows = list('Year and Month FE' = c('Y', "Y", "N", "N")),
       include.adjrs = FALSE,
       include.rmse = FALSE,
       file = paste0(out, '/elite_interactions.tex'))

stacked_data %>%
  select(issue:dv_index, pid_simple, yob, raceeth, educ_3, type) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  group_by(name) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time + pid_simple + type,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  filter(term %in% c('prob_traceeasy', 'simplicitysimple', 'solve_prob', 'solu_traceeasy', 'cobenefitscosts', 'time')) %>%
  mutate(covar = 'covariates') %>%
  bind_rows(stacked_data %>%
              select(issue:dv_index) %>%
              pivot_longer(cols=starts_with('dv_')) %>%
              group_by(name) %>%
              do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time,
                                fixed_effects = ~ issue, 
                                data = .))) %>%
              mutate(covar = 'no covariates')) %>%
  mutate(term = vars_rename(term),
         name = vars_rename(name)) %>% 
  ggplot() + 
  geom_hline(yintercept=0, lty=2) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.96*std.error), ymax=(estimate + 1.96*std.error), col=covar), position = position_dodge(width=.5)) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.65*std.error), ymax=(estimate + 1.65*std.error), col=covar), fatten=2, size=1.1, position = position_dodge(width=.5)) + 
  coord_flip() +  
  facet_wrap(~name) + 
  labs(x='', y = paste0('Effect of Policy Attributes')) +
  theme_classic() +
  scale_color_grey()+
  theme(legend.position='bottom', legend.title=element_blank()) 
ggsave(paste0(out, '/elite_covar.pdf'), height=5, width=5)

r1 <- lm_robust(dv_ease ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time + pid_simple + type,
                fixed_effects = ~ issue, 
                data = stacked_data)
r2 <- lm_robust(dv_elections ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time + pid_simple + type,
                fixed_effects = ~ issue, 
                data = stacked_data)
r3 <- lm_robust(dv_likely ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time + pid_simple + type,
                fixed_effects = ~ issue, 
                data = stacked_data)
r4 <- lm_robust(dv_priority ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time + pid_simple + type,
                fixed_effects = ~ issue, 
                data = stacked_data)
r5 <- lm_robust(dv_index ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time + pid_simple + type,
                fixed_effects = ~ issue, 
                data = stacked_data)


texreg(list(r5, r1, r2, r3, r4),
       stars = NULL,
       digits = 3,
       include.ci=F,
       omit.coef = 'Intercept',
       custom.model.names = c('Index', "Importance", "Approval", "Likelihood", "Priority"),
       #custom.coef.names=c('Staff Recommendation', 'Commenter Preference', 'Positive', 'Negative', 'Neutral', 'Time Trend'),
       caption = '\\textbf{Tabular Presentation of Elite Experiment -- Covariate Adjusted Models}',
       #groups = list('Comments' = 3:5),
       label = "tab:elite_covar",
       #custom.gof.rows = list('Year and Month FE' = c('Y', "Y", "N", "N")),
       include.adjrs = FALSE,
       include.rmse = FALSE,
       file = paste0(out, '/elite_main.tex'))


ggplot(reg_data %>% filter(name=='Index')) + 
  geom_hline(yintercept=0, lty=2) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.96*std.error), ymax=(estimate + 1.96*std.error), col = type), position = position_dodge(width=.5)) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.65*std.error), ymax=(estimate + 1.65*std.error), col = type), fatten=2, size=1.1, position = position_dodge(width=.5)) + 
  coord_flip() +  
  #facet_wrap(~name) + 
  labs(x='', y = '') +
  theme_classic() +
  scale_color_grey() + 
  #scale_y_continuous(breaks=c(-.2,0,.2)) + 
  theme(legend.position='bottom', legend.title=element_blank()) 
ggsave(paste0(out,'/elite_role.pdf'), height=5, width=5)

print(xtable(reg_data %>% 
               mutate(estimate = round(estimate, 3),
                      std.error = round(std.error, 3)) %>% 
               select(DV = name, Role = type, Coefficient = term, Estimate = estimate, `Std. Error` = std.error),
            caption = '\\textbf{Tabular Presentation of Elite Experiment by Role}',
            caption.above = T,
            label = "tab:elite_role"),
      file = paste0(out, '/elite_role.tex'),
      include.rownames=FALSE)


reg_data <- stacked_data %>%
  select(issue:dv_index, party) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  mutate(pid_simple = case_when(str_detect(party, 'Democrat') ~ "D",
                                str_detect(party, 'Republican') ~ "R",
                                str_detect(party, 'Independent') ~ "I")) %>%
  group_by(name, pid_simple) %>%
  filter(pid_simple %in% c("D", "R")) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  mutate(term = vars_rename(term),
         name = vars_rename(name))
ggplot(reg_data %>% filter(name=="Index")) + 
  geom_hline(yintercept=0, lty=2) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.96*std.error), ymax=(estimate + 1.96*std.error), col = pid_simple), position = position_dodge(width=.5)) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.65*std.error), ymax=(estimate + 1.65*std.error), col = pid_simple), fatten=2, size=1.1, position = position_dodge(width=.5)) + 
  coord_flip() +  
  #facet_wrap(~name) + 
  labs(x='', y = '') +
  theme_classic() +
  scale_color_manual(values = c("D" = 'Blue', "R" = 'Red')) + 
  #scale_y_continuous(breaks=c(-.2,0,.2)) + 
  theme(legend.position='bottom', legend.title=element_blank())  
ggsave(paste0(out,'/elite_party.pdf'), height=5, width=5)

party_int <- lm_robust(dv_index ~ prob_trace*pid_simple + simplicity*pid_simple + solve_prob*pid_simple + solu_trace*pid_simple + cobenefits*pid_simple + time*pid_simple,
          fixed_effects = ~ issue, 
          data = stacked_data) %>%
  tidy()

print(xtable(party_int %>% 
               mutate(estimate = round(estimate, 3),
                      std.error = round(std.error, 3)) %>% 
               select(Coefficient = term, Estimate = estimate, `Std. Error` = std.error),
             caption = '\\textbf{Tabular Presentation of Heterogeneous Effects by Party}',
             caption.above = T,
             label = "tab:party_int"),
      file = paste0(out, '/party_int.tex'),
      include.rownames=FALSE)

stargazer(reg_data %>% 
            select(DV = name, PID = pid_simple, Coefficient = term, estimate, std.error) %>%
            mutate(estimate = round(estimate, 3),
                   std.error = round(std.error, 3)), 
          summary=F, out = paste0(out, '/elite_party.tex'), style='apsr', digits=2, rownames=F, 
          title = '\\textbf{Tabular Presentation of Elite Experiment by Party}',
          label = "tab:elite_party")

  
# competitiveness
reg_data <- stacked_data %>%
  filter(type=='legislator') %>% 
  filter(!is.na(dv_index)) %>% 
  select(issue:dv_index, competitive) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  filter(!is.na(competitive)) %>% 
  group_by(name, competitive) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  mutate(term = vars_rename(term),
         name = vars_rename(name),
         comp = ifelse(competitive==F, 'Uncompetitive Seats', 'Competitive Seats')) 
ggplot(reg_data %>% filter(name=='Index')) + 
  geom_hline(yintercept=0, lty=2) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.96*std.error), ymax=(estimate + 1.96*std.error), col = comp), position = position_dodge(width=.5)) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.65*std.error), ymax=(estimate + 1.65*std.error), col = comp), fatten=2, size=1.1, position = position_dodge(width=.5)) + 
  coord_flip() +  
  #facet_wrap(~name) + 
  labs(x='', y = '') +
  theme_classic() +
  scale_color_grey() + 
  #scale_y_continuous(breaks=c(-.2,0,.2)) + 
  theme(legend.position='bottom', legend.title=element_blank())  
ggsave(paste0(out,'/elite_competitive.pdf'), height=5, width=5)

reg_data <- stacked_data %>%
  filter(type=='legislator') %>% 
  filter(!is.na(dv_index)) %>% 
  mutate(margin2 = as.numeric(str_remove_all(reelect_margin, '^((?![0-9]).)*$')),
         competitive2 = ifelse(margin2<=10, 1, 0)) %>% 
  select(issue:dv_index, competitive2) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  filter(!is.na(competitive2)) %>% 
  group_by(name, competitive2) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time,
                    fixed_effects = ~ issue, 
                    data = .))) %>%
  mutate(term = vars_rename(term),
         name = vars_rename(name),
         comp = ifelse(competitive2==F, 'Uncompetitive Seats', 'Competitive Seats')) 
ggplot(reg_data %>% filter(name=='Index')) + 
  geom_hline(yintercept=0, lty=2) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.96*std.error), ymax=(estimate + 1.96*std.error), col = comp), position = position_dodge(width=.5)) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.65*std.error), ymax=(estimate + 1.65*std.error), col = comp), fatten=2, size=1.1, position = position_dodge(width=.5)) + 
  coord_flip() +  
  #facet_wrap(~name) + 
  labs(x='', y = '') +
  theme_classic() +
  scale_color_grey() + 
  #scale_y_continuous(breaks=c(-.2,0,.2)) + 
  theme(legend.position='bottom', legend.title=element_blank())  
ggsave(paste0(out,'/elite_competitive.pdf'), height=5, width=5)

reg_data <- stacked_data %>%
  select(issue:dv_index) %>%
  pivot_longer(cols=starts_with('dv_')) %>%
  filter(!is.na(issue)) %>%
  group_by(name, issue) %>%
  do(tidy(lm_robust(value ~ prob_trace + simplicity + solve_prob + solu_trace + cobenefits + time,
                    data = .))) %>%
  filter(term!='(Intercept)') %>%
  mutate(term = vars_rename(term),
         name = vars_rename(name)) 
ggplot(reg_data %>% filter(name=='Index')) + 
  geom_hline(yintercept=0, lty=2) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.96*std.error), ymax=(estimate + 1.96*std.error)), position = position_dodge(width=.5)) + 
  geom_pointrange(aes(x=term, y=estimate, ymin = (estimate - 1.65*std.error), ymax=(estimate + 1.65*std.error)), fatten=2, size=1.1, position = position_dodge(width=.5)) + 
  coord_flip() +  
  facet_wrap(~issue) + 
  labs(x='', y = '') +
  theme_classic() +
  scale_y_continuous(breaks=c(-.2,0,.2)) + 
  theme(legend.position='bottom', legend.title=element_blank())  
ggsave(paste0(out,'/elite_issue.pdf'), height=5, width=5)

stargazer(reg_data %>% 
            select(issue, DV = name, Coefficient = term, estimate, std.error) %>%
            mutate(estimate = round(estimate, 3),
                   std.error = round(std.error, 3)), 
          summary=F, out = paste0(out, '/elite_issue.tex'), style='apsr', digits=2, rownames=F, 
          title = '\\textbf{Tabular Presentation of Elite Experiment by Issue}',
          label = "tab:elite_issue")

